import request from '@/utils/request';
import { ref } from 'vue';

export function requestGet(url: any = ''): any {
  return fetchFn(url, 'get');
}

export function requestPost(url: any = ''): any {
  return fetchFn(url, 'post');
}

export async function requsetAll(arr: any[] = []): Promise<any> {
  const res: any = await Promise.all(arr);

  //  通过遍历 - 提示错误信息

  return res;
}

//  高阶函数 - 柯里化
function fetchFn(url: any, method: any = 'get') {
  const func = (<any>request)[method];

  const _data = ref({}) as any;

  //  内部回调函数 - 用户更新数据
  async function setData(data: any = {}) {
    //  清空原数据
    _data.value = {};
    const res = await func({ url,  data });
    _data.value = res || {};

    return _data.value;
  }

  return [ _data, setData ];
}